package ui.activity;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import application.App;
import base.BaseBean;
import base.activity.BaseFragmentActivity;
import bean.ListOfPapersBean;
import student.andlei.com.student.R;
import ui.fragment.homepagefragment.doworkfragment.DoTheImageWorkFragment;
import ui.fragment.homepagefragment.doworkfragment.TestPaperFragment;
import utils.GsonUtils;
import utils.SPUtils;
import utils.StringUtils;
import utils.TimeUtil;
import utils.TimerUtil;
import view.ConfirmPopWindow;

public class DoTheImageWorkActivity extends BaseFragmentActivity{
    /*****************计时器*******************/
    private Runnable timeRunable = new Runnable() {
        @Override
        public void run() {
            currentSecond = currentSecond + 1000;
            tv_time.setText(TimeUtil.getFormatHMS(currentSecond));
            if (!isPause) {
                //递归调用本runable对象，实现每隔一秒一次执行任务
                mhandle.postDelayed(this, 1000);
            }
        }
    };
    //计时器
    private Handler mhandle = new Handler();
    private boolean isPause = false;//是否暂停
    private long currentSecond = 0;//当前毫秒数
    /*****************计时器*******************/
    private TextView tv_time;
    private TextView tv_dati,
                    tv_caogaozhi;
    public static List<ListOfPapersBean.mList.Questions> mLists = new ArrayList<>();
    public static List<ListOfPapersBean.Paper_image> paperimg_list = new ArrayList<>();
    private Fragment testpaperFragment;
    private Fragment dotheimageworkFragment;
    private View layoutview;
    public static TextView tv_numbr;
    private int paper_id;//试卷id
    private int paper_question_id;//题目id
    public static int student_task_id; // 学生作业id
    public int  task_id = 0;
    public static int answer_id;//答题卡id
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public void success(String action, BaseBean baseBean) {
        if(baseBean.code == 200){
            if (action.equals("Task/getAllQuestion")) {
                ListOfPapersBean bean = (ListOfPapersBean) GsonUtils.getInstance(this).parseComplexJArrayByCommon(baseBean, ListOfPapersBean.class);
                student_task_id = bean.student_task_id;
                answer_id = bean.answer_id;
                paperimg_list = bean.paper_images;
                if (!StringUtils.isNull(SPUtils.getInstance(App.getInstance()).getString(answer_id + "worktime"))) {
                    currentSecond = TimerUtil.GetS(SPUtils.getInstance(App.getInstance()).getString(answer_id + "worktime"));
                }
                timeRunable.run();
                if (bean.list == null) {
                    toast("暂无数据");
                    finish();
                    return;
                }
                mLists.clear();
                for (int i = 0; i < bean.list.size(); i++) {
                    //bean.list.get(i).type_name
                    for (int j = 0; j < bean.list.get(i).questions.size(); j++) {
                        bean.list.get(i).questions.get(j).type_id = bean.list.get(i).type_id;
                        bean.list.get(i).questions.get(j).type_name = bean.list.get(i).type_name;
                    }

                    mLists.addAll(bean.list.get(i).questions);
                }
                if(mLists.size()<=0){
                    toast("暂无数据");
                    finish();
                }
                for (int h = 0; h < mLists.size(); h++) {
                    for (int n = 0; n < mLists.get(h).options.size(); n++) {
                        if (!StringUtils.isNull(mLists.get(h).answer)) {
                            if (mLists.get(h).answer.equals(mLists.get(h).options.get(n).idx)) {
                                mLists.get(h).options.get(n).isSelect = 1;
                            } else {
                                mLists.get(h).options.get(n).isSelect = 0;
                            }
                        }
                    }
//                    if (!StringUtils.isNull(mLists.get(h).answer_image)) {
//                        String[] img_arry = mLists.get(h).answer_image.split("|");
//                        for (int i = 0; i < img_arry.length; i++) {
//                            if (!StringUtils.isNull(img_arry[i])) {
//                                LocalMedia localMedia = new LocalMedia();
//                                localMedia.setCompressPath(img_arry[i]);
//                                localMedia.setPath(img_arry[i]);
//                                mLists.get(h).imglist = new ArrayList<>();
//                                mLists.get(h).imglist.add(localMedia);
//                            }
//                        }
//                    }
//                    if (!StringUtils.isNull(mLists.get(h).drawing_board_image)) {
//                        mLists.get(h).shouxieimg = mLists.get(h).drawing_board_image;
//                        // loadImageSimpleTarget(mLists.get(h).shouxieimg);
//                    }
                }
                testpaperFragment = TestPaperFragment.getInstance();
                dotheimageworkFragment = DoTheImageWorkFragment.getInstance();
                addFragment(R.id.details_layout,testpaperFragment);
                addFragment(R.id.details_layout,dotheimageworkFragment);
                hideAllFragment();
                showFragment(testpaperFragment);
            }
        }
    }

    @Override
    public void error(String action, Throwable e) {

    }

    @Override
    protected int getLayoutId() {
        layoutview = View.inflate(context, R.layout.activity_dothework, null);
        return R.layout.activity_dotheimagework;
    }

    @Override
    protected void findViews() {
        tv_numbr = getView(R.id.tv_number);
        tv_time = getView(R.id.tv_time);
        tv_dati = getView(R.id.tv_answersheet);
        tv_caogaozhi = getView(R.id.tv_Scrappaper);
    }

    @Override
    protected void formatViews() {
        tv_dati.setOnClickListener(this);
        tv_caogaozhi.setOnClickListener(this);
    }

    @Override
    protected void formatData() {
        loadDataList();
    }

    @Override
    protected void getBundle(Bundle bundle) {
        paper_id = bundle.getInt("paper_id");
        student_task_id = bundle.getInt("student_task_id");
        task_id = bundle.getInt("task_id");
        setTitle("" + bundle.getString("title"));
    }
    public void jumpToTsetPaper(){
        hideAllFragment();
        if(testpaperFragment == null){
            testpaperFragment = TestPaperFragment.getInstance();
        }
        showFragment(testpaperFragment);
    }
    public void jumpToDoTheImageWork(){
        hideAllFragment();
        if(dotheimageworkFragment == null){
            dotheimageworkFragment = TestPaperFragment.getInstance();
        }
        showFragment(dotheimageworkFragment);
    }
    private void loadDataList() {
        Map<String, String> map = new HashMap<>();
        if(student_task_id>0){
            map.put("student_task_id", "" + student_task_id);
        }else {
            map.put("task_id",task_id+"");
        }
        post("Task/getAllQuestion", map, BaseBean.class, false);

    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_answersheet:
                Bundle bundle = new Bundle();
                bundle.putInt("student_task_id", student_task_id);
                bundle.putInt("answer_id", answer_id);
                jumpTo(AnswerSheetActivity.class, bundle);
                break;
            case R.id.tv_Scrappaper:
                new ConfirmPopWindow(this).showAtBottom(layoutview);
                break;
        }
    }
    //隐藏所有Fragment
    public void hideAllFragment() {
        hideFragment(testpaperFragment);
        hideFragment(dotheimageworkFragment);
    }
}
